import os
import json
import random
import uuid

async def create_database():
    
    if not os.path.exists("data"):
        os.makedirs("data")
    if not os.path.exists("图片文件"):
        os.makedirs("图片文件")
    if not os.path.exists("临时图片"):
        os.makedirs("临时图片")
    if not os.path.exists("临时图片/面板"):
        os.makedirs("临时图片/面板")
    with open("data/配置.txt", 'w') as f:
        f.write('{"探索间隔": 30, "复活间隔": 180, "休息间隔": 120}')
    if not os.path.exists("data/全局变量.txt"):
        with open("data/全局变量.txt", 'w') as f:
            f.write('{"剧情": {}, "物品": {}}')
    if not os.path.exists("data/player"):
        os.makedirs("data/player")

    #更新加入体力条#
    directory = "data/player/"
    for filename in os.listdir(directory):
        if filename.endswith(".txt"):
            filepath = os.path.join(directory, filename)
            with open(filepath, 'r') as f:
                data = json.load(f)
            if "赠送限制" not in data:
                data["赠送限制"] = {"总额度": 0, "分额度": {}}
            if "进化诅咒" not in data:
                data["进化诅咒"] = []
            if "体力" not in data:
                data["体力"] = 30 + data["等级"]
            if "副职业" not in data:
                data["副职业"] = []
            if "每日任务记录" not in data:
                data["每日任务记录"] = {}
            if "好感度" not in data:
                data["好感度"] = {}
            if "剧情任务记录" not in data:
                data["剧情任务记录"] = {}
            if "任务" not in data:
                data["任务"] = []
            if "生命回复" not in data:
                data["生命回复"] = 0
            if "魔力回复" not in data:
                data["魔力回复"] = 0
            if "物理减伤" not in data:
                data["物理减伤"] = 0
            if "魔法减伤" not in data:
                data["魔法减伤"] = 0
            if "物理穿透" not in data:
                data["物理穿透"] = 0
            if "魔法穿透" not in data:
                data["魔法穿透"] = 0
            if "物理增伤" not in data:
                data["物理增伤"] = 0
            if "魔法增伤" not in data:
                data["魔法增伤"] = 0
            if "最低命中率" not in data:
                data["最低命中率"] = 0
            if "最低闪避率" not in data:
                data["最低闪避率"] = 0
            if "最低暴击率" not in data:
                data["最低暴击率"] = 0
            if "保底" not in data:
                data["保底"] = {}
            if "进化" not in data["力量"]:
                data["力量"]["进化"] = 0
            if "进化" not in data["智力"]:
                data["智力"]["进化"] = 0
            if "进化" not in data["体质"]:
                data["体质"]["进化"] = 0
            if "进化" not in data["敏捷"]:
                data["敏捷"]["进化"] = 0
            for task in data["任务"]:
                if "编码" not in task:
                    data["任务"].remove(task)
                if "来源" not in task:
                    data["任务"].remove(task)
            with open(filepath, 'w') as f:
                json.dump(data, f, ensure_ascii=False, indent=4)
                
    if not os.path.exists("data/bag"):
        os.makedirs("data/bag")
    if not os.path.exists("data/battle"):
        os.makedirs("data/battle")
    if not os.path.exists("data/draw"):
        os.makedirs("data/draw")
    if not os.path.exists("data/player.txt"):
        with open("data/player.txt", 'w') as f:
            f.write('[]')
    if not os.path.exists("data/team.txt"):
        with open("data/team.txt", 'w') as f:
            f.write('[]')
    with open("data/body.txt", 'w') as f:
        f.write('[{"id": 1, "名称": "体质残疾", "种类": "体质", "需求": "小于体质0"}, {"id": 2, "名称": "力量残疾", "种类": "体质", "需求": "小于力量0"}, {"id": 3, "名称": "智力残疾", "种类": "体质", "需求": "小于智力0"}, {"id": 4, "名称": "敏捷残疾", "种类": "体质", "需求": "小于敏捷0"}, {"id": 5, "名称": "体质欠缺", "种类": "体质", "需求": "小于体质1"}, {"id": 6, "名称": "力量欠缺", "种类": "体质", "需求": "小于力量1"}, {"id": 7, "名称": "智力欠缺", "种类": "体质", "需求": "小于智力1"}, {"id": 8, "名称": "敏捷欠缺", "种类": "体质", "需求": "小于敏捷1"}, {"id": 9, "名称": "天生强壮", "种类": "体质", "需求": "大于体质3"}, {"id": 10, "名称": "天生聪明", "种类": "体质", "需求": "大于智力3"}, {"id": 11, "名称": "天生大力", "种类": "体质", "需求": "大于力量3"}, {"id": 12, "名称": "天生灵活", "种类": "体质", "需求": "大于敏捷3"}, {"id": 13, "名称": "天生强健", "种类": "体质", "需求": "大于体质5"}, {"id": 14, "名称": "天生神力", "种类": "体质", "需求": "大于力量5"}, {"id": 15, "名称": "天生聪慧", "种类": "体质", "需求": "大于智力5"}, {"id": 16, "名称": "天生灵巧", "种类": "体质", "需求": "大于敏捷5"}, {"id": 17, "名称": "轮回之体", "种类": "体质", "需求": "大于体质8"}, {"id": 18, "名称": "斗战之体", "种类": "体质", "需求": "大于力量8"}, {"id": 19, "名称": "神灵之体", "种类": "体质", "需求": "大于智力8"}, {"id": 20, "名称": "瞬之体", "种类": "体质", "需求": "大于敏捷8"}, {"id": 21, "名称": "轮回神体", "种类": "体质", "需求": "大于体质11"}, {"id": 22, "名称": "斗战神体", "种类": "体质", "需求": "大于力量11"}, {"id": 23, "名称": "仙灵之体", "种类": "体质", "需求": "大于智力11"}, {"id": 24, "名称": "影之体", "种类": "体质", "需求": "大于敏捷11"}, {"id": 25, "名称": "荒古圣体", "种类": "体质", "需求": "大于体质15"}, {"id": 26, "名称": "苍天霸体", "种类": "体质", "需求": "大于力量15"}, {"id": 27, "名称": "先天道体", "种类": "体质", "需求": "大于智力15"}, {"id": 28, "名称": "末影黑体", "种类": "体质", "需求": "大于敏捷15"}, {"id": 101, "名称": "人王血脉", "种类": "血脉", "需求": "力量3 智力3 敏捷3 体质3"}, {"id": 102, "名称": "青龙血脉", "种类": "血脉", "需求": "力量4 体质4 敏捷4"}, {"id": 103, "名称": "朱雀血脉", "种类": "血脉", "需求": "智力4 体质4 敏捷4"}, {"id": 104, "名称": "白虎血脉", "种类": "血脉", "需求": "力量6 敏捷6"}, {"id": 105, "名称": "玄武血脉", "种类": "血脉", "需求": "智力4 体质8"}, {"id": 106, "名称": "人皇血脉", "种类": "血脉", "需求": "力量5 智力5 敏捷5 体质5"}, {"id": 107, "名称": "青龙神血", "种类": "血脉", "需求": "力量6 体质6 敏捷6"}, {"id": 108, "名称": "朱雀神血", "种类": "血脉", "需求": "智力6 体质6 敏捷6"}, {"id": 109, "名称": "白虎神血", "种类": "血脉", "需求": "力量9 敏捷9"}, {"id": 110, "名称": "玄武神血", "种类": "血脉", "需求": "智力6 体质12"}, {"id": 201, "名称": "金手指", "种类": "天赋", "需求": "智力20"}, {"id": 202, "名称": "永动鸡", "种类": "天赋", "需求": "体质20"}, {"id": 203, "名称": "飞毛腿", "种类": "天赋", "需求": "敏捷20"}, {"id": 204, "名称": "大力出奇迹", "种类": "天赋", "需求": "力量20"}]')

    with open("data/赠送限制.txt", 'w') as f:
        f.write('{"铜币": 1, "银币": 100, "金币": 10000, "小兽肉": 2, "兽肉": 10, "大兽肉": 50, "小兽皮": 2, "兽皮": 10, "大兽皮": 50, "小兽骨": 2, "兽骨": 10, "大兽骨": 50, "小兽筋": 2, "兽筋": 10, "大兽筋": 50, "兽牙": 20, "天空粉尘": 1000, "银苹果": 3000, "金苹果": 30000, "体魄觉醒针": 40000, "神性的血滴": 120000, "进化灵药α": 20000, "进化灵药β": 20000, "进化灵药γ": 20000, "未知的精血": 1000, "闪亮的精血": 2000, "精灵之血": 5000, "淡黄色精血": 1200, "黄色精血": 2000, "淡绿色精血": 1200, "绿色精血": 2000, "淡红色精血": 1200, "红色精血": 2000, "淡蓝色精血": 1200, "蓝色精血": 2000, "青铜宝箱": 500, "白银宝箱": 2000, "黄金宝箱": 10000, "魔引香": 1000, "哥布林族徽": 100, "哥布林勋章": 1000, "球兔精血": 50, "巨兔精血": 100, "腐鸦精血": 50, "野猪精血": 50, "巨猪精血": 100, "森蚺精血": 50, "巨蚺精血": 100, "哥布林精血": 50, "黄金兔精血": 1000, "巨魔精血": 300, "缥缈之冠": 15000, "轻舞之肩": 15000, "灵动之束": 15000, "引风之摆": 15000, "飘逸之履": 15000, "智识之冕": 15000, "神藏之衣": 15000, "眷力腰带": 15000, "深邃之围": 15000, "坚毅之根": 15000, "魔神头盔": 15000, "魔神铠甲": 15000, "魔神腰带": 15000, "魔神护腿": 15000, "魔神甲鞋": 15000, "先贤之指引": 30000, "先贤之加护": 30000, "先贤之伟力": 30000, "暗之礼赞": 75000, "光之颂歌": 75000, "破灭万法之符": 75000, "死神的凝视": 75000, "黎明锋刃": 75000, "魔狼利爪": 75000, "不动如山": 75000, "闪亮的祝福": 200, "精灵之泪": 1000, "血肉残渣": 2, "能量余烬": 20, "血肉残骸": 20, "能量余辉": 200, "小血瓶": 5, "小魔瓶": 5, "强效小血瓶": 6, "强效小魔瓶": 6, "中血瓶": 15, "中魔瓶": 15, "强效中血瓶": 20, "强效中魔瓶": 20, "大血瓶": 50, "大魔瓶": 50, "强效大血瓶": 60, "强效大魔瓶": 60, "草莓": 10, "蓝莓": 10, "秩序碎片": 1500, "魔岩": 50, "冰晶石": 50, "魔化藤蔓": 50, "魔化之羽": 50, "硬皮": 50, "甲壳": 50, "木棍": 1000, "树枝": 1000, "骨棒": 2000, "骨盾": 2000, "强化木棍": 3000, "魔力树枝": 3000, "强化骨棒": 4000, "强化骨盾": 4000, "天空粉尘": 1000, "天空碎片": 5000, "天空晶石": 30000, "小火球·技能书": 200, "火球·技能书": 500, "大火球·技能书": 2000, "奋击·技能书": 500, "速击·技能书": 500, "缠击·技能书": 500, "嘲讽·技能书": 800, "治愈·技能书": 600, "群体治愈·技能书": 1200, "防御反击·技能书": 7200, "鼓舞·技能书": 1000, "振奋·技能书": 1000, "明睛·技能书": 1000, "物理加护·技能书": 2500, "物理庇护·技能书": 7200, "魔法加护·技能书": 2500, "魔法庇护·技能书": 7200, "魔法吟咏·技能书": 12000, "聚精会神·技能书": 12000, "全神贯注·技能书": 12000, "便携式传送门": 200, "世界树信标": 100, "白晶": 400, "绿晶": 1000, "蓝晶": 2500, "劣等硬刺": 400, "中等硬刺": 1000, "上等硬刺": 2500, "劣等角质": 400, "中等角质": 1000, "上等角质": 2500}')


    with open("data/exp.txt", 'w') as f:
        f.write('[{"等级": 0, "经验": 1}, {"等级": 1, "经验": 3}, {"等级": 2, "经验": 10}, {"等级": 3, "经验": 15}, {"等级": 4, "经验": 20}, {"等级": 5, "经验": 25}, {"等级": 6, "经验": 30}, {"等级": 7, "经验": 40}, {"等级": 8, "经验": 50}, {"等级": 9, "经验": 60}, {"等级": 10, "经验": 100}, {"等级": 11, "经验": 200}, {"等级": 12, "经验": 300}, {"等级": 13, "经验": 400}, {"等级": 14, "经验": 500}, {"等级": 15, "经验": 600}, {"等级": 16, "经验": 700}, {"等级": 17, "经验": 800}, {"等级": 18, "经验": 900}, {"等级": 19, "经验": 1000}, {"等级": 20, "经验": 1500}, {"等级": 21, "经验": 2000}, {"等级": 22, "经验": 2500}, {"等级": 23, "经验": 3000}, {"等级": 24, "经验": 3500}, {"等级": 25, "经验": 4000}, {"等级": 26, "经验": 5000}, {"等级": 27, "经验": 6000}, {"等级": 28, "经验": 7000}, {"等级": 29, "经验": 8000}, {"等级": 30, "经验": 10000}, {"等级": 31, "经验": 12000}, {"等级": 32, "经验": 14000}, {"等级": 33, "经验": 16000}, {"等级": 34, "经验": 18000}, {"等级": 35, "经验": 20000}, {"等级": 36, "经验": 25000}, {"等级": 37, "经验": 30000}, {"等级": 38, "经验": 35000}, {"等级": 39, "经验": 40000}, {"等级": 40, "经验": 50000}]')


    with open("data/make.txt", 'w') as f:
        f.write('[{"名称": "小血瓶", "配方": [{"血肉残渣": 1, "能量余烬": 1}]}, {"名称": "中血瓶", "配方": [{"血肉残渣": 5, "能量余烬": 1}]}, {"名称": "大血瓶", "配方": [{"血肉残渣": 25, "能量余烬": 1}]}, {"名称": "小魔瓶", "配方": [{"能量余烬": 2}]}, {"名称": "中魔瓶", "配方": [{"能量余烬": 5}]}, {"名称": "大魔瓶", "配方": [{"能量余烬": 12}]}, {"名称": "兽肉", "配方": [{"小兽肉": 5, "能量余烬": 1}]}, {"名称": "兽骨", "配方": [{"小兽骨": 5, "能量余烬": 1}]}, {"名称": "兽筋", "配方": [{"小兽筋": 5, "能量余烬": 1}]}, {"名称": "兽牙", "配方": [{"小兽牙": 5, "能量余烬": 1}]}, {"名称": "兽皮", "配方": [{"小兽皮": 5, "能量余烬": 1}]}, {"名称": "魔化之羽", "配方": [{"腐化之羽": 10, "能量余烬": 1}]}, {"名称": "魔岩铠甲", "配方": [{"魔岩": 10, "兽骨": 10, "能量余烬": 10}]}, {"名称": "魔岩护腿", "配方": [{"魔岩": 10, "兽骨": 10, "能量余烬": 10}]}, {"名称": "晶石头盔", "配方": [{"冰晶石": 10, "甲壳": 3, "能量余烬": 10}]}, {"名称": "晶石腰带", "配方": [{"冰晶石": 10, "兽筋": 10, "能量余烬": 10}]}, {"名称": "晶石鞋", "配方": [{"冰晶石": 10, "兽皮": 10, "能量余烬": 10}]}, {"名称": "木叶上衣", "配方": [{"宽叶": 10, "兽筋": 10, "能量余烬": 10}]}, {"名称": "木叶长裤", "配方": [{"宽叶": 10, "兽筋": 10, "能量余烬": 10}]}, {"名称": "羽枝头冠", "配方": [{"魔化之羽": 10, "腐化之羽": 10, "能量余烬": 10}]}, {"名称": "藤蔓腰带", "配方": [{"魔化藤蔓": 10, "兽筋": 10, "能量余烬": 10}]}, {"名称": "轻羽靴", "配方": [{"魔化之羽": 10, "腐化之羽": 10, "能量余烬": 10}]}, {"名称": "硬皮上衣", "配方": [{"硬皮": 5, "甲壳": 5, "兽骨": 10, "能量余烬": 10}]}, {"名称": "硬皮长裤", "配方": [{"硬皮": 5, "甲壳": 5, "兽骨": 10, "能量余烬": 10}]}, {"名称": "柔皮腰带", "配方": [{"兽皮": 50, "兽筋": 10, "能量余烬": 10}]}, {"名称": "甲皮兜帽", "配方": [{"硬皮": 5, "甲壳": 5, "兽骨": 10, "能量余烬": 10}]}, {"名称": "翘皮靴", "配方": [{"硬皮": 5, "甲壳": 5, "兽皮": 10, "能量余烬": 10}]}, {"名称": "魔力树枝", "配方": [{"树枝": 1, "魔化之羽": 10, "魔化藤蔓": 10, "能量余烬": 50}]}, {"名称": "强化木棍", "配方": [{"木棍": 1, "冰晶石": 10, "魔化藤蔓": 10, "能量余烬": 50}]}, {"名称": "骨棒", "配方": [{"兽骨": 40, "兽牙": 10, "能量余烬": 10}]}, {"名称": "强化骨棒", "配方": [{"骨棒": 1, "兽骨": 80, "兽牙": 20, "能量余烬": 50}]}, {"名称": "骨盾", "配方": [{"甲壳": 5, "兽骨": 20, "能量余烬": 10}]}, {"名称": "强化骨盾", "配方": [{"骨盾": 1, "魔岩": 10, "兽骨": 40, "能量余烬": 50}]}, {"名称": "陈旧的强化装置", "配方": [{"黯淡的强化装置": 1, "天空粉尘": 1, "能量余烬": 500}]}, {"名称": "黏糊糊的手环", "配方": [{"史莱姆凝胶": 10, "史莱姆黏液": 66, "能量余烬": 100}]}, {"名称": "滑溜溜的戒指", "配方": [{"史莱姆凝胶": 10, "史莱姆黏液": 66, "能量余烬": 100}]}, {"名称": "湿漉漉的项链", "配方": [{"史莱姆凝胶": 10, "史莱姆黏液": 66, "能量余烬": 100}]}, {"名称": "爱心手环", "配方": [{"黏糊糊的手环": 1, "史莱姆之心": 1, "史莱姆凝胶": 66, "史莱姆黏液": 666, "能量余烬": 666}]}, {"名称": "爱心戒指", "配方": [{"滑溜溜的戒指": 1, "史莱姆之心": 1, "史莱姆凝胶": 66, "史莱姆黏液": 666, "能量余烬": 666}]}, {"名称": "爱心项链", "配方": [{"湿漉漉的项链": 1, "史莱姆之心": 1, "史莱姆凝胶": 66, "史莱姆黏液": 666, "能量余烬": 666}]}, {"名称": "史莱姆黏液", "配方": [{"史莱姆粘液": 1}]}, {"名称": "新春祝福·金", "配方": [{"新春祝福·银": 9}]}, {"名称": "缥缈之冠", "配方": [{"智识之冕": 1}, {"秩序碎片": 10}]}, {"名称": "魔神头盔", "配方": [{"智识之冕": 1}, {"秩序碎片": 10}]}, {"名称": "轻舞之肩", "配方": [{"破法之衫": 1}, {"秩序碎片": 10}]}, {"名称": "神藏之衣", "配方": [{"破法之衫": 1}, {"秩序碎片": 10}]}, {"名称": "魔神铠甲", "配方": [{"破法之衫": 1}, {"秩序碎片": 10}]}, {"名称": "眷力腰带", "配方": [{"灵动之束": 1}, {"秩序碎片": 10}]}, {"名称": "魔神腰带", "配方": [{"灵动之束": 1}, {"秩序碎片": 10}]}, {"名称": "引风之摆", "配方": [{"深邃之袍": 1}, {"秩序碎片": 10}]}, {"名称": "深邃之围", "配方": [{"深邃之袍": 1}, {"秩序碎片": 10}]}, {"名称": "魔神护腿", "配方": [{"深邃之袍": 1}, {"秩序碎片": 10}]}, {"名称": "坚毅之根", "配方": [{"飘逸之履": 1}, {"秩序碎片": 10}]}, {"名称": "魔神甲鞋", "配方": [{"飘逸之履": 1}, {"秩序碎片": 10}]}, {"名称": "先贤之加护", "配方": [{"秩序碎片": 20}]}, {"名称": "先贤之伟力", "配方": [{"秩序碎片": 20}]}, {"名称": "先贤之指引", "配方": [{"秩序碎片": 20}]}, {"名称": "暗之礼赞", "配方": [{"秩序碎片": 50}]}, {"名称": "光之颂歌", "配方": [{"秩序碎片": 50}]}, {"名称": "魔狼利爪", "配方": [{"秩序碎片": 50}]}, {"名称": "黎明锋刃", "配方": [{"秩序碎片": 50}]}, {"名称": "死神的凝视", "配方": [{"秩序碎片": 50}]}, {"名称": "不动如山", "配方": [{"秩序碎片": 50}]}, {"名称": "破灭万法之符", "配方": [{"秩序碎片": 50}]}, {"名称": "劣等刺甲", "配方": [{"劣等硬刺": 10, "劣等角质": 10}]}, {"名称": "中等刺甲", "配方": [{"硬刺": 15, "角质": 15, "劣等硬刺": 30, "劣等角质": 30}, {"劣等刺甲": 1, "硬刺": 15, "角质": 15, "劣等硬刺": 20, "劣等角质": 20}]}, {"名称": "上等刺甲", "配方": [{"上等硬刺": 20, "上等角质": 20, "硬刺": 40, "角质": 40, "劣等硬刺": 100, "劣等角质": 100},{"中等刺甲": 1, "上等硬刺": 20, "上等角质": 20, "硬刺": 25, "角质": 25, "劣等硬刺": 70, "劣等角质": 70}]}]')
    

    if not os.path.exists("data/pet"):
        os.makedirs("data/pet")
    with open("data/pet/pet.txt", 'w') as f:
        f.write('[{"名称": "狐", "基础": {"体质": 3, "敏捷": 6, "力量": 3, "智力": 8}, "成长基准": {"体质": 0.2, "敏捷": 0.2, "力量": 0.1, "智力": 0.5}, "成长当前": {"体质": 0.2, "敏捷": 0.2, "力量": 0.1, "智力": 0.5}, "技能":[{"等级": 0, "名称": "狐假虎威", "类别": "被动", "简介": ""}]}]')
    


    with open("data/每日祈祷.txt", 'w') as f:
        f.write('{"财富":[{"名称": "铜币", "数量": 188, "概率": 1}, {"名称": "铜币", "数量": 388, "概率": 3}, {"名称": "铜币", "数量": 555, "概率": 5}, {"名称": "铜币", "数量": 666, "概率": 10}, {"名称": "铜币", "数量": 888, "概率": 100}, {"名称": "铜币", "数量": 1111, "概率": 105}, {"名称": "铜币", "数量": 1888, "概率": 15}, {"名称": "铜币", "数量": 2888, "概率": 10}, {"名称": "铜币", "数量": 4444, "概率": 5}, {"名称": "铜币", "数量": 6666, "概率": 2}, {"名称": "铜币", "数量": 8888, "概率": 1}], "强大": [{"名称": "未知的精血", "数量": 1, "概率": 50}, {"名称": "未知的精血", "数量": 3, "概率": 10}, {"名称": "未知的精血", "数量": 5, "概率": 5}, {"名称": "精灵之血", "数量": 1, "概率": 10}, {"名称": "精灵之血", "数量": 2, "概率": 10}, {"名称": "精灵之血", "数量": 3, "概率": 1}, {"名称": "银苹果", "数量": 1, "概率": 3}, {"名称": "银苹果", "数量": 2, "概率": 10}, {"名称": "银苹果", "数量": 3, "概率": 1}, {"名称": "金苹果", "数量": 1, "概率": 1}, {"名称": "天罚符咒", "数量": 1, "概率": 5}, {"名称": "天罚符咒", "数量": 2, "概率": 5}, {"名称": "天罚符咒", "数量": 3, "概率": 5}, {"名称": "狂暴之血", "数量": 1, "概率": 5}, {"名称": "狂暴之血", "数量": 2, "概率": 5}, {"名称": "狂暴之血", "数量": 3, "概率": 5}], "幸运": [{"名称": "青铜宝箱", "数量": 1, "概率": 105}, {"名称": "青铜宝箱", "数量": 2, "概率": 15}, {"名称": "青铜宝箱", "数量": 3, "概率": 5}, {"名称": "白银宝箱", "数量": 1, "概率": 10}, {"名称": "白银宝箱", "数量": 2, "概率": 5}, {"名称": "黄金宝箱", "数量": 1, "概率": 3}, {"名称": "幸运星", "数量": 1, "概率": 100}]}')


    if not os.path.exists("data/冒险者公会_委托板.txt"):
        with open("data/冒险者公会_委托板.txt", 'w') as f:
            f.write('{"当前":[], "委托列表":[{"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "巨兔精血*10/巨猪精血*10/巨蚺精血*10"}], "奖励": "公会代币*110+声望*30", "概率": 10}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "巨兔精血*3/巨猪精血*3/巨蚺精血*3"}], "奖励": "公会代币*30+声望*15", "概率": 10}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "巨兔精血*1/巨猪精血*1/巨蚺精血*1"}], "奖励": "公会代币*10+声望*6", "概率": 10}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "球兔精血*10/哥布林精血*10/腐鸦精血*10/森蚺精血*10/野猪精血*10"}], "奖励": "公会代币*20+声望*10", "概率": 10}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "球兔精血*50/哥布林精血*50/腐鸦精血*50/森蚺精血*50/野猪精血*50"}], "奖励": "公会代币*110+声望*30", "概率": 10}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "河蟹精血*40/菇蛙精血*40/肥鲶精血*40/岩鼠精血*40"}], "奖励": "公会代币*110+声望*30", "概率": 10}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "球兔精血*3/哥布林精血*3/腐鸦精血*3/森蚺精血*3/野猪精血*3"}], "奖励": "公会代币*6+声望*5", "概率": 10}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "河蟹精血*5/菇蛙精血*5/肥鲶精血*5/岩鼠精血*5"}], "奖励": "公会代币*13+声望*10", "概率": 10}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "巨魔精血*1"}], "奖励": "公会代币*10+声望*10", "概率": 5}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "巨魔精血*3"}], "奖励": "公会代币*33+声望*25", "概率": 2}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "巨魔精血*10"}], "奖励": "公会代币*115+声望*60", "概率": 1}, {"名称": "野外清理", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "球兔*10/哥布林*10/腐鸦*10/森蚺*10/野猪*10"}], "奖励": "公会代币*10+声望*10", "概率": 10}, {"名称": "野外清理", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "河蟹*5/菇蛙*5/肥鲶*5/岩鼠*5/活石*5/诡石*4/岩巨基*3/水妖精*3"}], "奖励": "公会代币*20+声望*10", "概率": 10}, {"名称": "野外清理", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "河蟹*10/菇蛙*10/肥鲶*10/岩鼠*10/活石*10/诡石*8/岩巨基*5/水妖精*5"}], "奖励": "公会代币*40+声望*20", "概率": 10}, {"名称": "野外清理", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "森林史莱姆*1/岩石史莱姆*1"}], "奖励": "公会代币*20+声望*30", "概率": 10}, {"名称": "野外清理", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "刺毛猪*1"}], "奖励": "公会代币*50+声望*50", "概率": 10}, {"名称": "野外清理", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "球兔*5/哥布林*5/腐鸦*5/森蚺*5/野猪*5"}], "奖励": "公会代币*5+声望*5", "概率": 10}, {"名称": "野外清理", "来源": "冒险者公会", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "巨兔*2/巨猪*2/巨蚺*2"}], "奖励": "公会代币*10+声望*50", "概率": 10}, {"名称": "野外清理", "来源": "冒险者公会", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "巨兔*1/巨猪*1/巨蚺*1"}], "奖励": "公会代币*5+声望*30", "概率": 10}, {"名称": "野外清理", "来源": "冒险者公会", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "哥布林前哨*10/哥布林先锋*5/哥布林战士*3/哥布林弩手*3"}], "奖励": "公会代币*15+声望*20", "概率": 10}, {"名称": "野外清理", "来源": "冒险者公会", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "野猪骑手*6/巨魔战士*6/野猪重骑兵*3/巨魔重卫*3"}], "奖励": "公会代币*30+声望*30", "概率": 10}, {"名称": "野外清理", "来源": "冒险者公会", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "哥布林敢死队*5/哥布林精锐*2/哥布林炮手*2/哥布林巫师*2"}], "奖励": "公会代币*30+声望*40", "概率": 10}, {"名称": "野外清理", "来源": "冒险者公会", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "讨伐", "对象": "哥布林上校*1"}], "奖励": "公会代币*50+声望*60", "概率": 10}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "未知的精血*1/淡蓝色精血*1/淡红色精血*1/淡绿色精血*1/淡黄色精血*1"}], "奖励": "公会代币*15+声望*10", "概率": 10}, {"名称": "收集精血", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "闪亮的精血*1/蓝色精血*1/红色精血*1/绿色精血*1/黄色精血*1"}], "奖励": "公会代币*30+声望*30", "概率": 10}, {"名称": "资金募集", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "铜币*1000"}], "奖励": "公会代币*10+声望*5", "概率": 10}, {"名称": "资金募集", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "铜币*10000"}], "奖励": "公会代币*105+声望*20", "概率": 10}, {"名称": "资金募集", "来源": "冒险者公会", "类型": "委托", "目标": [{"类型": "收集", "对象": "铜币*100000"}], "奖励": "公会代币*1100+声望*100", "概率": 10}]}')


    if not os.path.exists("data/abyss"):
        os.makedirs("data/abyss")
    if not os.path.exists("data/abyss/生成设定.txt"):
        with open("data/abyss/生成设定.txt", 'w') as f:
            f.write('{"深渊一层": {"面积": 100, "岩壁规模": 1, "丛林数量": 20, "丛林规模": 6, "丛林生成间隔": 60, "浆果密度%": 10, "百年香密度%": 5, "遗迹数量": 10, "遗迹规模": 3, "遗迹数量": 10, "玄铁矿数量": 20, "玄铁矿生成间隔": 180, "玄铁矿上限": 40, "山铜矿数量": 12, "山铜矿生成间隔": 360, "山铜矿上限": 24, "秘银矿数量": 6, "秘银矿生成间隔": 720, "秘银矿上限": 12, "精金矿数量": 3, "精金矿生成间隔": 1440, "精金矿上限": 6, "遗物列表": {"星辰指南针": 1, "姬乳石": 3, "磷墨石": 3, "引兽笛": 1, "凝聚光的碎片": 1, "不随机的骰子": 1, "向阴之花": 1, "下雨的伞": 1}}}')


    with open("data/道具开箱.txt", 'w') as f:
        f.write('{"青铜宝箱": ["普通-铜币*99*100", "普通-铜币*199*100", "普通-铜币*299*50", "普通-铜币*399*50", "普通-铜币*666*25", "普通-铜币*888*25", "普通-小血瓶*10*150", "普通-小魔瓶*10*150", "普通-中血瓶*10*75", "普通-中魔瓶*10*75", "普通-大血瓶*10*25", "普通-大魔瓶*10*25", "稀有-闪亮的祝福*10*100", "珍奇-缥缈之冠*1*4", "珍奇-轻舞之肩*1*3", "珍奇-灵动之束*1*3", "珍奇-引风之摆*1*3", "珍奇-飘逸之履*1*3", "珍奇-智识之冕*1*4", "珍奇-神藏之衣*1*3", "珍奇-眷力腰带*1*3", "珍奇-深邃之围*1*3", "珍奇-坚毅之根*1*3", "珍奇-魔神头盔*1*4", "珍奇-魔神铠甲*1*3", "珍奇-魔神腰带*1*3", "珍奇-魔神护腿*1*3", "珍奇-魔神甲鞋*1*3"], "白银宝箱": ["普通-铜币*333*25", "普通-铜币*666*50", "普通-铜币*888*100", "普通-铜币*1111*75", "普通-铜币*1666*25", "普通-铜币*2888*25", "普通-中血瓶*20*100", "普通-中魔瓶*20*100", "普通-大血瓶*20*100", "普通-大魔瓶*20*100", "稀有-闪亮的祝福*15*80", "稀有-闪亮的祝福*20*40", "稀有-闪亮的祝福*25*10", "稀有-精灵之泪*1*30", "稀有-精灵之泪*2*10", "珍奇-缥缈之冠*1*7", "珍奇-轻舞之肩*1*7", "珍奇-灵动之束*1*7", "珍奇-引风之摆*1*7", "珍奇-飘逸之履*1*6", "珍奇-智识之冕*1*7", "珍奇-神藏之衣*1*7", "珍奇-眷力腰带*1*7", "珍奇-深邃之围*1*7", "珍奇-坚毅之根*1*6", "珍奇-魔神头盔*1*7", "珍奇-魔神铠甲*1*7", "珍奇-魔神腰带*1*7", "珍奇-魔神护腿*1*7", "珍奇-魔神甲鞋*1*6", "珍奇-先贤之指引*1*10", "珍奇-先贤之加护*1*10", "珍奇-先贤之伟力*1*10"], "黄金宝箱": ["普通-铜币*888*1", "普通-铜币*1666*2", "普通-铜币*1888*3", "普通-铜币*2888*4", "普通-铜币*6666*5", "普通-铜币*8888*5", "普通-大血瓶*30*5", "普通-大魔瓶*30*5", "稀有-闪亮的祝福*30*15", "稀有-闪亮的祝福*40*8", "稀有-闪亮的祝福*50*5", "稀有-闪亮的祝福*66*2", "稀有-精灵之泪*3*7", "稀有-精灵之泪*4*4", "稀有-精灵之泪*5*1", "珍奇-缥缈之冠*1*1", "珍奇-轻舞之肩*1*1", "珍奇-灵动之束*1*1", "珍奇-引风之摆*1*1", "珍奇-飘逸之履*1*1", "珍奇-智识之冕*1*1", "珍奇-神藏之衣*1*1", "珍奇-眷力腰带*1*1", "珍奇-深邃之围*1*1", "珍奇-坚毅之根*1*1", "珍奇-魔神头盔*1*1", "珍奇-魔神铠甲*1*1", "珍奇-魔神腰带*1*1", "珍奇-魔神护腿*1*1", "珍奇-魔神甲鞋*1*1", "珍奇-先贤之指引*1*2", "珍奇-先贤之加护*1*2", "珍奇-先贤之伟力*1*2", "珍奇-暗之礼赞*1*1", "珍奇-光之颂歌*1*1", "珍奇-破灭万法之符*1*1", "珍奇-死神的凝视*1*1", "珍奇-黎明锋刃*1*1", "珍奇-魔狼利爪*1*1", "珍奇-不动如山*1*1"]}')






async def game_update():
    directory = "data/shop/"
    for filename in os.listdir(directory):
        if filename.endswith(".txt"):
            if filename == "公会商店.txt" or filename == "炼金所商店.txt" or filename == "铁匠铺商店.txt" or filename == "分解屋商店.txt":
                continue
            filepath = os.path.join(directory, filename)
            with open(filepath, 'r') as f:
                data = json.load(f)
            # 清空当前商店
            data["当前"] = []
             # 计算所有概率的总和
            total_weight = sum(item["概率"] for item in data["清单"])
            # 计算每个物品的比重
            weights = [item["概率"] / total_weight for item in data["清单"]]
            selected_items = random.choices(data["清单"], weights=weights, k=6)
            # 将选中的物品添加到"当前"列表中
            for item in selected_items:
                data["当前"].append(item)
            with open(filepath, 'w') as f:
                json.dump(data, f, ensure_ascii=False, indent=4)
    
    with open("data/boss_locate.txt", 'r') as f:
        data = json.load(f)
    for data_item in data:
        item = random.choice(data_item["范围"])
        x = random.uniform(item["x1"] + 1, item["x2"] - 1)
        y = random.uniform(item["y1"] + 1, item["y2"] - 1)
        data_item["当前"]["x"] = int(x)
        data_item["当前"]["y"] = int(y)
        data_item["存活"] = "是"
    with open("data/boss_locate.txt", 'w') as f:
        json.dump(data, f, ensure_ascii=False, indent=4)

    with open("data/冒险者公会_委托板.txt", 'r') as f:
        data = json.load(f)
    for data_item in data["当前"]:
        if "剩余时间" in data_item:
            data_item["剩余时间"] -= 1
            if data_item["剩余时间"] <= 0:
                data["当前"].remove(data_item)
    for data_item in data["委托列表"]:
        ran = random.randint(1, 100)
        if ran <= data_item["概率"]:
            task = []
            task0 = {}
            for item in data_item["目标"]:
                task0["类型"] = item["类型"]
                target =  random.choice(item["对象"].split("/"))
                target_name = target.split("*")[0]
                target_number = int(target.split("*")[1])
                task0["对象"] = target_name
                task0["数量"] = target_number
                task.append(task0)
            reward = data_item["奖励"]
            time = random.choice([2, 3, 4, 5, 6])
            unique_code = uuid.uuid4().hex
            data["当前"].append({"名称": data_item["名称"], "来源": data_item["来源"], "类型": data_item["类型"], "目标": task, "奖励": reward, "剩余时间": time, "编码": unique_code})
    with open("data/冒险者公会_委托板.txt", 'w') as f:
        json.dump(data, f, ensure_ascii=False, indent=4)

    with open("data/player.txt", 'r') as f:
        player_data = json.load(f)
    for player in player_data:
        if player["状态"] == "野外商店" or player["状态"] == "野外神秘商店":
            player["状态"] == "行动"
    with open("data/player.txt", 'w') as f:
        json.dump(player_data, f, ensure_ascii=False, indent=4)

async def game_day_update():
    directory = "data/player/"
    for filename in os.listdir(directory):
        if filename.endswith(".txt"):
            filepath = os.path.join(directory, filename)
            with open(filepath, 'r') as f:
                data = json.load(f)
            if "赠送限制" in data:
                value = int(data["等级"]) * 1000
                if data["赠送限制"]["总额度"] > value:
                    data["赠送限制"]["总额度"] -= value
                elif data["赠送限制"]["总额度"] < -value:
                    data["赠送限制"]["总额度"] += value
                else:
                    data["赠送限制"]["总额度"] = 0
            data["体力"] = 30 + data["等级"]
            if "副职业" in data:
                for job in data["副职业"]:
                    job["精力"] = job["精力上限"]
            data["每日任务记录"] = {}
            with open(filepath, 'w') as f:
                json.dump(data, f, ensure_ascii=False, indent=4)
    with open("data/team.txt", 'r') as f:
        team_data = json.load(f)
    team_data = []
    with open("data/team.txt", 'w') as f:
        json.dump(team_data, f, ensure_ascii=False, indent=4)
    with open("data/player.txt", 'r') as f:
        player_data = json.load(f)
    for player in player_data:
        player["队伍中"] = "否"
        if "验证码日次数" in player:
            player["验证码日次数"] = 0
    with open("data/player.txt", 'w') as f:
        json.dump(player_data, f, ensure_ascii=False, indent=4)
    


async def jinhua_zuzhou():
    
    #进化诅咒补丁#
    directory = "data/player/"
    for filename in os.listdir(directory):
        if filename.endswith(".txt"):
            filepath = os.path.join(directory, filename)
            with open(filepath, 'r') as f:
                data = json.load(f)
            if "灵阶" not in data:
                data["灵阶"] = 1
            if data["灵阶"] == 3:
                data["进化诅咒"] = []
                for evolute in data["进化"]:
                    if random.random() < 0.5:
                        with open(f"data/进化诅咒.txt", 'r') as f:
                            unevolute = json.load(f)
                        unevolute = [d for d in unevolute if d['部位'] == evolute["部位"]]
                        random_unevolute = random.choice(unevolute)
                        if data["进化诅咒"]:
                            while True:
                                for every_unevolute in data["进化诅咒"]:
                                    if random_unevolute["部位"] == every_unevolute["部位"] and random_unevolute["词条"] == every_unevolute["词条"]:
                                        random_unevolute = random.choice(unevolute)
                                        break
                                else:
                                    break

                        num_range = random_unevolute["数值"].split("-")
                        num_min = float(num_range[0])
                        num_max = float(num_range[1])
                        random_num = round(random.uniform(num_min, num_max), 2)
                        random_unevolute["数值"] = random_num
                        data["进化诅咒"].append(random_unevolute)
                with open(filepath, 'w') as f:
                    json.dump(data, f, ensure_ascii=False, indent=4)



        
    